package com.chry.ocr.redis;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.apache.log4j.Logger;
import org.mybatis.caches.redis.SerializeUtil;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import com.chry.ocr.common.RedisUtil;
public class RedisCache implements Cache {
	
	private Logger logger =  Logger.getLogger(RedisCache.class);
	private static final String operation = "MybatisCache";
	private String id;
	//读写锁
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	
	private static JedisConnectionFactory jedisConnectionFactory;

	
	public RedisCache(String id){
		if(id==null){
			throw new IllegalArgumentException("require an id");
		}
		logger.debug("redisCache:id="+id+"---------");
		this.id=id;
	}
	/*
	 * 清除所有数据
	 * 
	 */
	@Override
	public void clear() {
		JedisConnection connection = null;
		try {
			connection = (JedisConnection)jedisConnectionFactory.getConnection();
			connection.flushAll();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection!=null){
				connection.close();
			}
		}
	}
	/*
	 * 得到指定key的value
	 * 
	 */
	@Override
	public Object getObject(Object key) {
		Object result = null;
		JedisConnection connection = null;
		try {
			connection = (JedisConnection)jedisConnectionFactory.getConnection();
			//result = SerializeUtils.unserialize(connection.get(SerializeUtils.serialize(key)));
			result = SerializeUtil.unserialize(connection.get(SerializeUtil.serialize(RedisUtil.keyGenerator(operation,id+key.toString()))));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection!=null){
				connection.close();
			}
		}
		return result;
	}

	/*
	 * 得到当前db的key值
	 *
	 * 
	 */
	@Override
	public int getSize() {
		int result = 0;
		JedisConnection connection = null;
		try {
			connection = (JedisConnection)jedisConnectionFactory.getConnection();
			result = Integer.valueOf(connection.dbSize().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection!=null){
				connection.close();
			}
		}
		return result;
	}
	/*
	 * 写入 key-value
	 * 
	 */
	@Override
	public void putObject(Object key, Object value) {
		JedisConnection connection = null;
		try {
			logger.debug("redis put object:"+key.toString()+":"+value.toString());
			connection = (JedisConnection)jedisConnectionFactory.getConnection();
			//connection.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
			connection.set(SerializeUtil.serialize(RedisUtil.keyGenerator(operation,id+key.toString())), SerializeUtil.serialize(value));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection!=null){
				connection.close();
			}
		}
		
	}
/*
 * 删除指定的key
 * @see org.apache.ibatis.cache.Cache#removeObject(java.lang.Object)
 */
	@Override
	public Object removeObject(Object key) {
		JedisConnection connection = null;
		Object result = null;
				try {
					connection = (JedisConnection)jedisConnectionFactory.getConnection();
					//result = connection.expire(SerializeUtils.serialize(key), 0);
					result = connection.expire(SerializeUtil.serialize(RedisUtil.keyGenerator(operation,id+key.toString())), 0);
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(connection!=null){
						connection.close();
					}
				}
		return result;
	}
	
	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public ReadWriteLock getReadWriteLock() {
		// TODO Auto-generated method stub
		return this.readWriteLock;
	}
	/*
	 * 注入jedisConnectionFactory
	 */
	public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
		RedisCache.jedisConnectionFactory = jedisConnectionFactory;
	}

}
